#include "Hud.h"
#include "../Utility/Renderer/GeometryGenerator.h"
#include "ShaderManager.h"
#include "../Utility/Misc/Timer.h"

Mesh		Hud::mesh[2];
Material	Hud::material[2];
XMMATRIX	Hud::world[2];

float		Hud::healthWidthMax;
float		Hud::ammoWidthMax;
float		Hud::height;
float		Hud::currHealth = 100.0f;
float		Hud::maxHealth = 100.0f;
Timer		timer;

// NOTE: Orthogonal matrix goes from 0 to 1 on all planes
void Hud::Initialize()
{
	timer.Init();
	
	healthWidthMax = 0.25f;
	ammoWidthMax = 0.25f;

	height = 0.025f;
	// initialize mesh
	mesh[0].CreateBox(healthWidthMax, height, height);
	mesh[1].CreateBox(ammoWidthMax, height, height);
	//mesh[0].CreatePlane(healthWidthMax, height);

	// initialize material color
	material[0].color[0] = 0.0f; material[0].color[1] = 1.0f; material[0].color[2] = 0.0f; material[0].color[3] = 1.0f;
	material[1].color[0] = 0.0f; material[1].color[1] = 0.0f; material[1].color[2] = 1.0f; material[1].color[3] = 1.0f;

	//initialize world transform
	XMFLOAT3 position;
	XMFLOAT3 scale;
	XMFLOAT4 scaleOrientation;
	XMFLOAT3 scaleOrigin;
	XMFLOAT3 rotationOrigin;
	XMFLOAT4 rotationQuaternion;

	position			= XMFLOAT3(0.15f,0.1f,0.0f);
	scale				= XMFLOAT3(1.0f,1.0f,1.0f);
	scaleOrientation	= XMFLOAT4(0.0f,0.0f,0.0f,0.0f);
	scaleOrigin			= XMFLOAT3(.125f,0.0f,.125f);
	rotationOrigin		= XMFLOAT3(0.0f,0.0f,0.0f);
	rotationQuaternion	= XMFLOAT4(0.1f,0.1f,0.0f,0.0f);

	XMVECTOR auxPosition			= XMLoadFloat3(&position);
	XMVECTOR auxScale				= XMLoadFloat3(&scale);
	XMVECTOR auxScaleOrientation	= XMLoadFloat4(&scaleOrientation);
	XMVECTOR auxScaleOrigin			= XMLoadFloat3(&scaleOrigin);
	XMVECTOR auxRotationOrigin		= XMLoadFloat3(&rotationOrigin);
	XMVECTOR auxRotationQuaternion	= XMLoadFloat4(&rotationQuaternion);

	world[0] = XMMatrixTransformation(	auxScaleOrigin, auxScaleOrientation, auxScale,
										auxRotationOrigin, auxRotationQuaternion,
										auxPosition);

	position			= XMFLOAT3(0.15f,0.068f,0.0f);
	scale				= XMFLOAT3(1.0f,1.0f,1.0f);
	scaleOrientation	= XMFLOAT4(0.0f,0.0f,0.0f,0.0f);
	scaleOrigin			= XMFLOAT3(0.0f,0.0f,0.0f);
	rotationOrigin		= XMFLOAT3(0.0f,0.0f,0.0f);
	rotationQuaternion	= XMFLOAT4(0.1f,0.1f,0.0f,0.0f);

	auxPosition				= XMLoadFloat3(&position);
	auxScale				= XMLoadFloat3(&scale);
	auxScaleOrientation		= XMLoadFloat4(&scaleOrientation);
	auxScaleOrigin			= XMLoadFloat3(&scaleOrigin);
	auxRotationOrigin		= XMLoadFloat3(&rotationOrigin);
	auxRotationQuaternion	= XMLoadFloat4(&rotationQuaternion);

	world[1] = XMMatrixTransformation(	auxScaleOrigin, auxScaleOrientation, auxScale,
										auxRotationOrigin, auxRotationQuaternion,
										auxPosition);
}

void Hud::Render()
{
	//Health
	XMMATRIX result = XMMatrixMultiply(world[0], ShaderManager::GetOrtho());
	ShaderManager::SetOrtho(result);
	mesh[0].RenderIndexedPrimitive();
	ShaderManager::RenderColorOrtho(mesh[0],material[0]);

	//Ammo
	result = XMMatrixMultiply(world[1], ShaderManager::GetOrtho());
	ShaderManager::SetOrtho(result);
	mesh[1].RenderIndexedPrimitive();
	ShaderManager::RenderColorOrtho(mesh[1],material[1]);

	timer.TimeStep();

	float floatTimerHelper = (float)timer.GetDeltaTime();
	floatTimerHelper/=1000.0f; // to seconds
	currHealth -= floatTimerHelper*10.0f; // tic per sec

	XMFLOAT3 position;
	XMFLOAT3 scale;
	XMFLOAT4 scaleOrientation;
	XMFLOAT3 scaleOrigin;
	XMFLOAT3 rotationOrigin;
	XMFLOAT4 rotationQuaternion;

	position			= XMFLOAT3(0.15f,0.1f,0.0f);
	scale				= XMFLOAT3(currHealth/maxHealth,1.0f,1.0f);
	scaleOrientation	= XMFLOAT4(0.0f,0.0f,0.0f,0.0f);
	scaleOrigin			= XMFLOAT3(-0.125f,0.0f,-0.125f);
	rotationOrigin		= XMFLOAT3(0.0,0.0f,0.0f);
	rotationQuaternion	= XMFLOAT4(0.1f,0.1f,0.0f,0.0f);

	XMVECTOR auxPosition			= XMLoadFloat3(&position);
	XMVECTOR auxScale				= XMLoadFloat3(&scale);
	XMVECTOR auxScaleOrientation	= XMLoadFloat4(&scaleOrientation);
	XMVECTOR auxScaleOrigin			= XMLoadFloat3(&scaleOrigin);
	XMVECTOR auxRotationOrigin		= XMLoadFloat3(&rotationOrigin);
	XMVECTOR auxRotationQuaternion	= XMLoadFloat4(&rotationQuaternion);

	world[0] = XMMatrixTransformation(	auxScaleOrigin, auxScaleOrientation, auxScale,
		auxRotationOrigin, auxRotationQuaternion,
		auxPosition);
}
